#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <mutex>

int main()
{
	std::mutex _mtx;
	std::condition_variable c;
	int n = 100;
	int flag = true;

	std::thread t1([&]()
		{
			int i = 0;
			while (i < n)
			{
				std::unique_lock<std::mutex> lock(_mtx);
				while (!flag)
				{
					c.wait(lock);
				}

				std::cout << i << std::endl;
				flag = false;
				i += 2;
				c.notify_one();
			}
		}
	);

	std::thread t2([&]()
		{
			int i = 1;
			while (i < n)
			{
				std::unique_lock<std::mutex> lock(_mtx);
				while (flag)
				{
					c.wait(lock);
				}
				std::cout << i << std::endl;
				flag = true;
				i += 2;
				c.notify_one();
			}
		}
	);

	t1.join();
	t2.join();
	
	return 0;
}